package com.nado.iims2.blog.bo.manage;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nado.iims2.blog.dao.manage.CodeDAO;
import com.nado.iims2.blog.model.manage.Category;
import com.nado.iims2.blog.model.manage.Code;

@Service("CodeBO")
public class CodeBOImpl implements CodeBO {
	/** The log. */
	private static Log log = LogFactory.getLog(CodeBOImpl.class);
	
	@Autowired
	private CodeDAO codeDAO = null;
	
	/**
	 * Gets category list.
	 * 
	 * @return
	 */
	public List<Category> getCategoryList() {
		try {
			List<Category> result = new ArrayList<Category>();
			result = codeDAO.selectCategoryList();
			return result;
		} catch (Exception e) {
			log.error(e, e);
			return new ArrayList<Category>();
		}
	}
	
	/**
	 * Gets code list.
	 * 
	 * @param catCd
	 * @return
	 */
	public List<Code> getCodeList(String categoryCd) {
		try {
			List<Code> result = new ArrayList<Code>();
			result = codeDAO.selectCodeList(categoryCd);
			return result;
		} catch (Exception e) {
			log.error(e, e);
			return new ArrayList<Code>();
		}
	}

	/**
	 * Gets category list count.
	 * 
	 * @return
	 */
	@Override
	public int getCategoryListCount() {
		try {
			return codeDAO.selectCategoryListCount();
		} catch (Exception e) {
			log.error(e, e);
			return 0;
		}
	}
	
	/**
	 * Write category info.
	 * 
	 * @param model
	 * @return
	 */
	@Override
	public int writeCategory(Category model) {
		int result = 0;
		try {
			if(model.isRegist()) {
				result = codeDAO.insertCategory(model);
			} else {
				result = codeDAO.updateCategory(model);
			}
		} catch (Exception e) {
			log.error(e, e);
			return -1;
		}
		return result;
	}
	
	/**
	 * Write code info.
	 * 
	 * @param model
	 * @return
	 */
	@Override
	public int writeCode(Code model) {
		int result = 0;
		try {
			if(model.isRegist()) {
				result = codeDAO.insertCode(model);
			} else {
				result = codeDAO.updateCode(model);
			}
		} catch (Exception e) {
			log.error(e, e);
			return -1;
		}
		return result;
	}

	/**
	 * Remove code info.
	 * 
	 * @param model
	 * @return
	 */
	@Override
	public int removeCode(Code model) {
		int result = 0;
		try {
			result = codeDAO.deleteCode(model);
		} catch (Exception e) {
			log.error(e, e);
		}
		return result;
	}

	@Override
	public int getByCategoryCd(String categoryCd) {
		int result = 0;
		try {
			result = codeDAO.selectByCategoryCd(categoryCd);
		} catch (Exception e) {
			log.error(e, e);
			return -1;
		}
		return result;
	}

	@Override
	public int getByCodeCd(String categoryCd, String code) {
		int result = 0;
		try {
			result = codeDAO.selectByCodeCd(categoryCd, code);
		} catch (Exception e) {
			log.error(e, e);
			return -1;
		}
		return result;
	}
}
